package com.smart.util;

import java.util.List;
import java.util.Date;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.ArrayList;
import java.time.LocalDateTime;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.ConcurrentMap;
import org.springframework.util.StringUtils;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author lgb
 * @date 2020年6月13日 上午9:25:25
 * Copyright (C) 深圳工勘科技
 * @说明: To change this template use File | Settings | File Templates.
 **/
public class DateUtil {

    private static final ConcurrentMap<String, DateTimeFormatter> FORMATTER_CACHE = new ConcurrentHashMap<>();

    private static final int PATTERN_CACHE_SIZE = 500;
    
    /**
     * Date转换为格式化时间
     * @param date date
     * @param pattern 格式
     * @return
     **/
    public static String format(Date date, String pattern){
        return format(LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()), pattern);
    }


    public static String formatDate(Date date, String pattern){
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    /**
     * localDateTime转换为格式化时间
     * @param localDateTime localDateTime
     * @param pattern 格式
     * @return
     **/
    public static String format(LocalDateTime localDateTime, String pattern){
        DateTimeFormatter formatter = createCacheFormatter(pattern);
        return localDateTime.format(formatter);
    }
    public static String formatLongStr(LocalDateTime localDateTime){
    	DateTimeFormatter formatter = createCacheFormatter("yyyy-MM-dd HH:mm:ss");
        return localDateTime.format(formatter);
    }
    public static String formatLongStr(Date dateTime){
    	SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formatter.format(dateTime);
    }
    /**
     * 格式化字符串转为Date
     * @param time 格式化时间
     * @param pattern 格式
     * @return
     **/
    public static Date parseDate(String time, String pattern){
        return Date.from(parseLocalDateTime(time, pattern).atZone(ZoneId.systemDefault()).toInstant());

    }

    /**
     * 格式化字符串转为LocalDateTime
     * @param time 格式化时间
     * @param pattern 格式
     * @return
     **/
    public static LocalDateTime parseLocalDateTime(String time, String pattern){
        DateTimeFormatter formatter = createCacheFormatter(pattern);
        return LocalDateTime.parse(time, formatter);
    }
    /**
     * 在缓存中创建DateTimeFormatter
     * @param pattern 格式
     * @return
     **/
    private static DateTimeFormatter createCacheFormatter(String pattern){
        if (pattern == null || pattern.length() == 0) {
            throw new IllegalArgumentException("Invalid pattern specification");
        }
        DateTimeFormatter formatter = FORMATTER_CACHE.get(pattern);
        if(formatter == null){
            if(FORMATTER_CACHE.size() < PATTERN_CACHE_SIZE){
                formatter = DateTimeFormatter.ofPattern(pattern);
                DateTimeFormatter oldFormatter = FORMATTER_CACHE.putIfAbsent(pattern, formatter);
                if(oldFormatter != null){
                    formatter = oldFormatter;
                }
            }
        }
        return formatter;
    }

    public static String formatCurrentDateToShortStr(){
        LocalDateTime localDateTime=LocalDateTime.now();
        String pattern = "yyMMdd";
        return format(localDateTime,pattern);
    }
    public static String formatCurrentDateToLongStr(){
        LocalDateTime localDateTime=LocalDateTime.now();
        String pattern="yyyy-MM-dd HH:mm:ss";
        return format(localDateTime,pattern);
    }
    /**
     * 将时间字符串转化为  yyyy-MM-dd HH:mm:ss 时间
     * @param pstrString
     * @return
     **/
    public static Date stringToDate(String pstrString){
        if(StringUtils.isEmpty(pstrString)){
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date toDate = null;
        try {
            toDate = sdf.parse(pstrString);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return toDate;
    }

    public static String formatTime(String date){
        if(StringUtils.isEmpty(date)){
            return null;
        }
        return date.substring(0, date.length() - 11);
    }

    public static String formatTime2(String date){
        if(StringUtils.isEmpty(date)){
            return null;
        }
        return date.substring(0, date.length() - 2);
    }

	public static List<String> getIntervalTimeList(String start,String end,int interval){
		Date startDate = convertString2Date("yyyy-MM-dd HH:mm:ss",start);
		Date endDate = convertString2Date("yyyy-MM-dd HH:mm:ss",end);
		List<String> list = new ArrayList<>();
		while(startDate.getTime()<=endDate.getTime()){
			list.add(convertDate2String("yyyy-MM-dd HH:mm:ss",startDate));
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(startDate);
			calendar.add(Calendar.HOUR,interval);
			if(calendar.getTime().getTime()>endDate.getTime()){
				if(!startDate.equals(endDate)){
					list.add(convertDate2String("yyyy-MM-dd HH:mm:ss",endDate));
				}
				startDate = calendar.getTime();
			}else{
				startDate = calendar.getTime();
			}

		}
		return list;
	}

	private static Date convertString2Date(String format, String dateStr) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		try {
			Date date = simpleDateFormat.parse(dateStr);
			return date;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	private static String convertDate2String(String format,Date date) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		return simpleDateFormat.format(date);
	}
}